package com.yehebl.orm.data.common;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.GeneratedKeyHolder;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * jdbc 简单操作
 *
 * @author tzd
 */
public class DsJdbcTemplate implements IJdbc {

    public DsJdbcTemplate(JdbcTemplate jdbcTemplate) {
        setJdbcTemplate(jdbcTemplate);
    }

    private JdbcTemplate jdbcTemplate;

    /**
     * 增删改
     *
     * @param sql sql语句
     * @return 影响条数
     */
    @Override
    public int update(String sql, Object... objs) throws SQLException {
        return jdbcTemplate.update(sql, objs);
    }


    @Override
    public int insert(final String sql, GeneratedKeyHolder gk, final Object... objs) throws SQLException {
        return jdbcTemplate.update(connection -> {
            PreparedStatement ps = connection.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);
            for (int i = 0; i < objs.length; i++) {
                ps.setObject(i + 1, objs[i]);
            }
            return ps;
        }, gk);
    }

    /**
     * 增删改
     *
     * @param sql sql语句
     * @return 影响条数
     */
    @Override
    public int update(String sql) throws SQLException {
        return jdbcTemplate.update(sql);
    }

    /**
     * 批量增删改
     *
     * @param sql  sql语句
     * @param objs 参数
     * @return 影响条数
     */
    @Override
    public int[] batchUpdate(String sql, Object[]... objs) {
        List<Object[]> objArray = new ArrayList<>(objs.length);
        Collections.addAll(objArray, objs);
        return jdbcTemplate.batchUpdate(sql, objArray);
    }

    /**
     * 查询
     *
     * @param sql  sql语句
     * @param objs 参数
     */
    @Override
    public <T> T query(String sql, final IResultSet<T> resultSet, Object... objs) throws Exception {
        return jdbcTemplate.query(sql, rs -> {
            try {
                return resultSet.result(rs);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }, objs);
    }

    /**
     * 查询
     *
     * @param sql sql语句
     */
    @Override
    public <T> T query(String sql, final IResultSet<T> resultSet) throws Exception {
        return jdbcTemplate.query(sql, rs -> {
            try {
                return resultSet.result(rs);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });
    }

    @Override
    public void execute(String sql) {
        jdbcTemplate.execute(sql);
    }


    @Override
    public Connection getConnection() {
        try {
            return Objects.requireNonNull(this.jdbcTemplate.getDataSource()).getConnection();
        } catch (SQLException e) {
            throw new RuntimeException("获取数据库连接失败：" + e.getMessage());
        }
    }


    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }


    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }


}
